package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingApi;
import com.tanhua.dubbo.api.mapper.SettingMapper;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SettingApiImpl implements SettingApi {

    @Autowired
    private SettingMapper settingMapper;

    @Override
    public Settings findUserById(Long id) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Settings settings = settingMapper.selectOne(queryWrapper);
        return settings;
    }

    /**
     * 通知设置 - 保存
     * @return
     */
    @Override
    public void save(Settings settings) {
        // 通过userId查询setting表中是否有数据
        Settings settingsInDb = findUserById(settings.getUserId());

        if(settingsInDb != null){
            // 更新数据
            settings.setId(settingsInDb.getId());
            settingMapper.updateById(settings);
        }else {
            // 插入数据
            settingMapper.insert(settings);
        }
    }
}
